package br.com.amadeus.android.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import br.com.amadeus.android.entities.Archive;
import br.com.amadeus.android.entities.Course;
import br.com.amadeus.android.entities.CourseUser;
import br.com.amadeus.android.entities.Forum;
import br.com.amadeus.android.entities.Message;
import br.com.amadeus.android.entities.Resource;
import br.com.amadeus.android.entities.SocialHistory;
import br.com.amadeus.android.entities.User;

public class JSONReader {
	
	protected static final String CATEGORIA = "curso";
		
	public static ArrayList<CourseUser> getListCourseUser(String coursesJson) throws JSONException, ParseException{
		ArrayList<CourseUser> courseUserList = new ArrayList<CourseUser>();
		
		JSONObject jObject2 = new JSONObject(coursesJson);
		JSONObject obj2 = jObject2.getJSONObject("service");
		try {
			JSONArray coursesArray = obj2.getJSONArray("listPersons");
			for (int i = 0; i < coursesArray.length(); i++) {
				
				JSONObject obj = coursesArray.getJSONObject(i);
				JSONObject objPerson = obj.getJSONObject("person");
				JSONObject objCourse = obj.getJSONObject("course");
				
				CourseUser courseUser = new CourseUser(0);
				courseUser.setUser(new User(objPerson.getInt("id")));
				courseUser.setCourse(new Course(objCourse.getInt("id")));
				courseUserList.add(courseUser);
				
			}
		} catch (Exception e) {
			JSONObject objPersons = obj2.getJSONObject("listPersons");
			JSONObject objPerson = objPersons.getJSONObject("person");
			JSONObject objCourse = objPersons.getJSONObject("course");
			
			CourseUser courseUser = new CourseUser(0);
			courseUser.setUser(new User(objPerson.getInt("id")));
			courseUser.setCourse(new Course(objCourse.getInt("id")));
			courseUserList.add(courseUser);
		}
		
		return courseUserList;
	}
	
	public static String getNamePerson(String jsonStr)throws JSONException, ParseException{
		
		String name;
		JSONObject jObject2 = new JSONObject(jsonStr);
		JSONObject obj2 = jObject2.getJSONObject("service");
		try {
			JSONArray jsonArray = obj2.getJSONArray("listPersons");

			JSONObject obj = jsonArray.getJSONObject(0);
			JSONObject objPerson = obj.getJSONObject("person");
			name = objPerson.getString("name");

		} catch (Exception e) {
			JSONObject jsonObj = obj2.getJSONObject("listPersons");
			JSONObject objPerson = jsonObj.getJSONObject("person");
			name = objPerson.getString("name");
			
			
			
		}
		return name;
	}
	
	public static  ArrayList<Course> getListCourses(String coursesJson, int countCourses) throws JSONException, ParseException{
		ArrayList<Course> listCourses = new ArrayList<Course>();
		ArrayList<Resource> resourceList;
		ArrayList<Message> listMessages = null;
						
		JSONObject jObject2 = new JSONObject(coursesJson);
		JSONObject obj2 = jObject2.getJSONObject("service");
		if (countCourses > 1) {
			JSONArray coursesArray = obj2.getJSONArray("listCourses");
			for (int i = 0; i < coursesArray.length(); i++) {
				resourceList = new ArrayList<Resource>();
								
				JSONObject obj = coursesArray.getJSONObject(i);
				Course course = new Course(0);
				course.setId_amadeus(obj.getInt("id"));
				course.setName(obj.getString("name"));
				course.setObjectives(obj.getString("objectives"));
				course.setContent(obj.getString("content"));
				course.setMaxAmountStudents(obj.getInt("maxAmountStudents"));
				course.setNumberOfStudentsInCourse(obj.getInt("numberOfStudentsInCourse"));
				String beginDate = obj.getString("initialCourseDate");
				String endDate = obj.getString("finalCourseDate");
				course.setBeginDate(Util.getDate(beginDate, Util.FORMATO_DATA_BD));
				course.setEndDate(Util.getDate(endDate, Util.FORMATO_DATA_BD));
				
				try {
					JSONArray modArray = obj.getJSONArray("materials");
					for (int j = 0; j < modArray.length(); j++) {						
						JSONObject mat = modArray.getJSONObject(j);
						Resource resource = new Resource(0);
						resource.setAmadeus_id(mat.getInt("id"));
						resource.setName(mat.getString("archiveName"));
						resource.setCourse(course);
						
						String matExtension = mat.getString("extension");
						String[] split = matExtension.split("\\.");
						String value = "";
						String extension = "";
						int length = split.length;
						if (length == 2) {
							value = split[0];
							extension = split[1];
						}else{
							value = split[0];
							extension = split[length - 1];
						}
							
						resource.setValue(value);
						resource.setExtension(extension);
						
						//resource.setCourse_amadeus_id(mat.getInt("course_id"));
					
						resourceList.add(resource);
					}
					
				} catch (Exception e) {
					try {
						JSONObject mat = obj.getJSONObject("materials");
						Resource resource = new Resource(0);
						resource.setAmadeus_id(mat.getInt("id"));
						resource.setName(mat.getString("archiveName"));
						resource.setCourse(course);
						
						String matExtension = mat.getString("extension");
						String[] split = matExtension.split("\\.");
						String value = "";
						String extension = "";
						int length = split.length;
						if (length == 2) {
							value = split[0];
							extension = split[1];
						}else{
							value = split[0];
							extension = split[length - 1];
						}
						
						resource.setValue(value);
						resource.setExtension(extension);
						
						//resource.setCourse_amadeus_id(mat.getInt("course_id"));
					
						resourceList.add(resource);
					} catch (Exception e2) {
						// TODO: handle exception
					}
					
				}
				
				try {
					JSONArray modArray2 = obj.getJSONArray("foruns");
					for (int n = 0; n < modArray2.length(); n++) {
						JSONObject jsonForum = modArray2.getJSONObject(n);
						Resource resource = new Resource(0);
												
						Forum forum = new Forum(0);
						forum.setAmadeus_id(jsonForum.getInt("id"));
						String creationDate = jsonForum.getString("creationDate");
						forum.setCreationDate(Util.getDate(creationDate, Util.FORMATO_DATA_BD));
						forum.setDescription(jsonForum.getString("description"));
						forum.setName(jsonForum.getString("name"));
																		
						try {
							listMessages = new ArrayList<Message>();
							JSONArray forumArray = jsonForum.getJSONArray("messages");
							for (int k = 0; k < forumArray.length(); k++) {
								JSONObject jsonMess = forumArray.getJSONObject(k);
								Message message = new Message(0);
								message.setAmadeus_id(jsonMess.getInt("id"));
								message.setBody(jsonMess.getString("body"));
								String date = jsonMess.getString("date");
								message.setDate(Util.getDate(date, Util.FORMATO_DATA_BD));
								message.setPersonName(jsonMess.getString("personName"));
								message.setPersonLogin(jsonMess.getString("personLogin"));
								message.setForum(new Forum());
								listMessages.add(message);
							}
						} catch (Exception e) {
							try {
								listMessages = new ArrayList<Message>();
								JSONObject forumObj = jsonForum.getJSONObject("messages");
								Message message = new Message(0);
								message.setAmadeus_id(forumObj.getInt("id"));
								message.setBody(forumObj.getString("body"));
								String date = forumObj.getString("date");
								message.setDate(Util.getDate(date, Util.FORMATO_DATA_BD));
								message.setPersonName(forumObj.getString("personName"));
								message.setPersonLogin(forumObj.getString("personLogin"));
								message.setForum(new Forum());
								listMessages.add(message);
							} catch (Exception e2) {
								
							}
							
						}
						
						forum.setListMessages(listMessages);
						resource.setForum(forum);
						resource.setName(forum.getName());
						resource.setAmadeus_id(forum.getAmadeus_id());
						resource.setCourse(course);
						resourceList.add(resource);
						
					}
				} catch (Exception e) {
					try {
						JSONObject modObj2 = obj.getJSONObject("foruns");
						Resource resource = new Resource(0);
						
						Forum forum = new Forum(0);
						forum.setAmadeus_id(modObj2.getInt("id"));
						String creationDate = modObj2.getString("creationDate");
						forum.setCreationDate(Util.getDate(creationDate, Util.FORMATO_DATA_BD));
						forum.setDescription(modObj2.getString("description"));
						forum.setName(modObj2.getString("name"));
																		
						try {
							listMessages = new ArrayList<Message>();
							JSONArray forumArray = modObj2.getJSONArray("messages");
							for (int k = 0; k < forumArray.length(); k++) {
								JSONObject jsonMess = forumArray.getJSONObject(k);
								Message message = new Message(0);
								message.setAmadeus_id(jsonMess.getInt("id"));
								message.setBody(jsonMess.getString("body"));
								String date = jsonMess.getString("date");
								message.setDate(Util.getDate(date, Util.FORMATO_DATA_BD));
								message.setPersonName(jsonMess.getString("personName"));
								message.setPersonLogin(jsonMess.getString("personLogin"));
								message.setForum(new Forum());
								listMessages.add(message);
							}
						} catch (Exception e2) {
							try {
								listMessages = new ArrayList<Message>();
								JSONObject forumObj = modObj2.getJSONObject("messages");
								Message message = new Message(0);
								message.setAmadeus_id(forumObj.getInt("id"));
								message.setBody(forumObj.getString("body"));
								String date = forumObj.getString("date");
								message.setDate(Util.getDate(date, Util.FORMATO_DATA_BD));
								message.setPersonName(forumObj.getString("personName"));
								message.setPersonLogin(forumObj.getString("personLogin"));
								message.setForum(new Forum());
								listMessages.add(message);
							} catch (Exception e3) {
								
							}
							
						}
						
						forum.setListMessages(listMessages);
						resource.setForum(forum);
						resource.setName(forum.getName());
						resource.setAmadeus_id(forum.getAmadeus_id());
						resource.setCourse(course);
						resourceList.add(resource);
					} catch (Exception e2) {
						// TODO: handle exception
					}
					
				}
				
				course.setResourceList(resourceList);
				listCourses.add(course);
			}
		}else if (countCourses == 1) {
						
			JSONObject courseObj = obj2.getJSONObject("listCourses");
			resourceList = new ArrayList<Resource>();
			Course course = new Course(0);
			course.setId_amadeus(courseObj.getInt("id"));
			course.setName(courseObj.getString("name"));
			course.setObjectives(courseObj.getString("objectives"));
			course.setContent(courseObj.getString("content"));
			course.setMaxAmountStudents(courseObj.getInt("maxAmountStudents"));
			course.setNumberOfStudentsInCourse(courseObj.getInt("numberOfStudentsInCourse"));
			String beginDate = courseObj.getString("initialCourseDate");
			String endDate = courseObj.getString("finalCourseDate");
			course.setBeginDate(Util.getDate(beginDate, Util.FORMATO_DATA_BD));
			course.setEndDate(Util.getDate(endDate, Util.FORMATO_DATA_BD));
					
			try {
				JSONArray modArray = courseObj.getJSONArray("materials");
				for (int j = 0; j < modArray.length(); j++) {
					JSONObject mat = modArray.getJSONObject(j);
					Resource resource = new Resource(0);
					resource.setAmadeus_id(mat.getInt("id"));
					resource.setName(mat.getString("archiveName"));
					resource.setCourse(course);
					
					String matExtension = mat.getString("extension");
					String[] split = matExtension.split("\\.");
					String value = "";
					String extension = "";
					int length = split.length;
					if (length == 2) {
						value = split[0];
						extension = split[1];
					}else{
						value = split[0];
						extension = split[length - 1];
					}
					
					resource.setValue(value);
					resource.setExtension(extension);
					
					//resource.setCourse_amadeus_id(mat.getInt("course_id"));
					
					resourceList.add(resource);
				}
			} catch (Exception e) {
				try {
					JSONObject matObj = courseObj.getJSONObject("materials");
					Resource resource = new Resource(0);
					resource.setAmadeus_id(matObj.getInt("id"));
					resource.setName(matObj.getString("archiveName"));
					resource.setCourse(course);
					
					String matExtension = matObj.getString("extension");
					String[] split = matExtension.split("\\.");
					String value = "";
					String extension = "";
					int length = split.length;
					if (length == 2) {
						value = split[0];
						extension = split[1];
					}else{
						value = split[0];
						extension = split[length - 1];
					}
					
					resource.setValue(value);
					resource.setExtension(extension);
					
					//resource.setCourse_amadeus_id(matObj.getInt("course_id"));
					
					resourceList.add(resource);
				} catch (Exception e2) {
					// TODO: handle exception
				}
				
			}
			
			try {
				JSONArray modArray2 = courseObj.getJSONArray("foruns");
				for (int n = 0; n < modArray2.length(); n++) {
					JSONObject jsonForum = modArray2.getJSONObject(n);
					Resource resource = new Resource(0);
					
					Forum forum = new Forum(0);
					forum.setAmadeus_id(jsonForum.getInt("id"));
					String creationDate = jsonForum.getString("creationDate");
					forum.setCreationDate(Util.getDate(creationDate, Util.FORMATO_DATA_BD));
					forum.setDescription(jsonForum.getString("description"));
					forum.setName(jsonForum.getString("name"));
										
					try {
						listMessages = new ArrayList<Message>();
						JSONArray forumArray = jsonForum.getJSONArray("messages");
						for (int k = 0; k < forumArray.length(); k++) {
							JSONObject jsonMess = forumArray.getJSONObject(k);
							Message message = new Message(0);
							message.setAmadeus_id(jsonMess.getInt("id"));
							message.setBody(jsonMess.getString("body"));
							String date = jsonMess.getString("date");
							message.setDate(Util.getDate(date, Util.FORMATO_DATA_BD));
							message.setPersonName(jsonMess.getString("personName"));
							message.setPersonLogin(jsonMess.getString("personLogin"));
							message.setForum(new Forum());
							listMessages.add(message);
						}
					} catch (Exception e) {
						try {
							listMessages = new ArrayList<Message>();
							JSONObject forumObj = jsonForum.getJSONObject("messages");
							Message message = new Message(0);
							message.setAmadeus_id(forumObj.getInt("id"));
							message.setBody(forumObj.getString("body"));
							String date = forumObj.getString("date");
							message.setDate(Util.getDate(date, Util.FORMATO_DATA_BD));
							message.setPersonName(forumObj.getString("personName"));
							message.setPersonLogin(forumObj.getString("personLogin"));
							message.setForum(new Forum());
							listMessages.add(message);
						} catch (Exception e2) {
							
						}
						
					}
					
					forum.setListMessages(listMessages);
					resource.setForum(forum);
					resource.setName(forum.getName());
					resource.setAmadeus_id(forum.getAmadeus_id());
					resource.setCourse(course);
					resourceList.add(resource);
				}
			} catch (Exception e) {
				try {
					JSONObject modObj2 = courseObj.getJSONObject("foruns");
					Resource resource = new Resource(0);
					
					Forum forum = new Forum(0);
					forum.setAmadeus_id(modObj2.getInt("id"));
					String creationDate = modObj2.getString("creationDate");
					forum.setCreationDate(Util.getDate(creationDate, Util.FORMATO_DATA_BD));
					forum.setDescription(modObj2.getString("description"));
					forum.setName(modObj2.getString("name"));
										
					try {
						listMessages = new ArrayList<Message>();
						JSONArray forumArray = modObj2.getJSONArray("messages");
						for (int k = 0; k < forumArray.length(); k++) {
							JSONObject jsonMess = forumArray.getJSONObject(k);
							Message message = new Message(0);
							message.setAmadeus_id(jsonMess.getInt("id"));
							message.setBody(jsonMess.getString("body"));
							String date = jsonMess.getString("date");
							message.setDate(Util.getDate(date, Util.FORMATO_DATA_BD));
							message.setPersonName(jsonMess.getString("personName"));
							message.setPersonLogin(jsonMess.getString("personLogin"));
							message.setForum(new Forum());
							listMessages.add(message);
						}
					} catch (Exception e2) {
						try {
							listMessages = new ArrayList<Message>();
							JSONObject forumObj = modObj2.getJSONObject("messages");
							Message message = new Message(0);
							message.setAmadeus_id(forumObj.getInt("id"));
							message.setBody(forumObj.getString("body"));
							String date = forumObj.getString("date");
							message.setDate(Util.getDate(date, Util.FORMATO_DATA_BD));
							message.setPersonName(forumObj.getString("personName"));
							message.setPersonLogin(forumObj.getString("personLogin"));
							message.setForum(new Forum());
							listMessages.add(message);
						} catch (Exception e3) {
							
						}
						
					}
					
					forum.setListMessages(listMessages);
					resource.setForum(forum);
					resource.setName(forum.getName());
					resource.setAmadeus_id(forum.getAmadeus_id());
					resource.setCourse(course);
					resourceList.add(resource);
				} catch (Exception e2) {
					// TODO: handle exception
				}
				
			}
			
			course.setResourceList(resourceList);
			listCourses.add(course);
		}
		

		return listCourses;
	}
		
	public static boolean validateAccessInfo(String jsonStr)throws JSONException{
		JSONObject jsonObj = new JSONObject(jsonStr);
		JSONObject obj = jsonObj.getJSONObject("service");
		boolean validate = obj.getBoolean("validAccessInfo");
		return validate;
	}
	
	public static int countCourses(String countStr) throws JSONException{
		JSONObject jsonObj = new JSONObject(countStr);
		JSONObject obj = jsonObj.getJSONObject("service");
		int count = obj.getInt("countCourses");
		return count;
	}
	
	public static Archive getArchive(String json) throws JSONException{
		JSONObject jsonObj = new JSONObject(json);
		JSONObject obj = jsonObj.getJSONObject("service");
		JSONObject arc = obj.optJSONObject("archive");
		Archive archive = new Archive();
		archive.setId(arc.getInt("id"));
		archive.setArchive(arc.getString("archive").getBytes());
		return archive;
		
	}
	
	public static ArrayList<SocialHistory> getAmadeusHistoric(String json) throws JSONException{
		ArrayList<SocialHistory> listHistoric = new ArrayList<SocialHistory>();
		try {
			JSONObject jsonObj = new JSONObject(json);
			JSONObject obj = jsonObj.getJSONObject("service");
			JSONArray objHist = obj.getJSONArray("listHistoric");
			for (int i = 0; i < objHist.length(); i++) {
				JSONObject hist = objHist.getJSONObject(i);
				SocialHistory amadHist = new SocialHistory(0);
				amadHist.setActionValue(hist.getString("actionValue"));
				amadHist.setCourseName(hist.getString("courseName"));
				amadHist.setUserName(hist.getString("userName"));
				amadHist.setUserLogin(hist.getString("userLogin"));
				amadHist.setResourceName(hist.getString("resourceName"));
				amadHist.setCreationDate(Util.getDate(hist.getString("creationDate"), Util.FORMATO_DATA_BD));
				amadHist.setAmadeusID(hist.getInt("id"));
				listHistoric.add(amadHist);
			}
		} catch (Exception e) {
			try {
				JSONObject jsonObj = new JSONObject(json);
				JSONObject obj = jsonObj.getJSONObject("service");
				JSONObject obj2 = obj.getJSONObject("listHistoric");
				SocialHistory amadHist = new SocialHistory(0);
				amadHist.setActionValue(obj2.getString("actionValue"));
				amadHist.setCourseName(obj2.getString("courseName"));
				amadHist.setUserName(obj2.getString("userName"));
				amadHist.setUserLogin(obj2.getString("userLogin"));
				amadHist.setResourceName(obj2.getString("resourceName"));
				amadHist.setCreationDate(Util.getDate(obj2.getString("creationDate"), Util.FORMATO_DATA_BD));
				amadHist.setAmadeusID(obj2.getInt("id"));
				listHistoric.add(amadHist);
			} catch (Exception e2) {
			}
			
		}
		
		return listHistoric;
	}
	
	public static ArrayList<Forum> getForumListMessage(String jsonArchive) throws JSONException{
		ArrayList<Forum> forumListMessage = new ArrayList<Forum>();
		ArrayList<Message> listMessage = null;
		JSONObject jsonObj = new JSONObject(jsonArchive);
		JSONObject obj = jsonObj.getJSONObject("service");
		try {
			JSONArray objList = obj.getJSONArray("listForum");
			for (int i = 0; i < objList.length(); i++) {
				JSONObject objForum = objList.getJSONObject(i);
				Forum forum = new Forum(0);
				forum.setName(objForum.getString("name"));
				forum.setDescription(objForum.getString("description"));
				forum.setCreationDate(Util.getDate(objForum.getString("creationDate"), Util.FORMATO_DATA_BD));
				forum.setAmadeus_id(objForum.getInt("id"));
				try {
					listMessage = new ArrayList<Message>();
					JSONArray objList2 = objForum.getJSONArray("messages");
					for (int j = 0; j < objList2.length(); j++) {
						JSONObject objMsg = objList2.getJSONObject(j);
						Message message = new Message(0);
						message.setAmadeus_id(objMsg.getInt("id"));
						message.setBody(objMsg.getString("body"));
						message.setDate(Util.getDate(objMsg.getString("date"), Util.FORMATO_DATA_BD));
						message.setPersonName(objMsg.getString("personName"));
						message.setPersonLogin(objMsg.getString("personLogin"));
						message.setForum(forum);
						listMessage.add(message);
					}
					forum.setListMessages(listMessage);
					forumListMessage.add(forum);
				} catch (Exception e) {
					JSONObject objMsg = objForum.getJSONObject("messages");
					listMessage = new ArrayList<Message>();
					Message message = new Message(0);
					message.setAmadeus_id(objMsg.getInt("id"));
					message.setBody(objMsg.getString("body"));
					message.setDate(Util.getDate(objMsg.getString("date"), Util.FORMATO_DATA_BD));
					message.setPersonName(objMsg.getString("personName"));
					message.setPersonLogin(objMsg.getString("personLogin"));
					message.setForum(forum);
					
					listMessage.add(message);
					forum.setListMessages(listMessage);
					forumListMessage.add(forum);
				}
			}
		} catch (Exception e) {
			try {
				JSONObject objForum = obj.getJSONObject("listForum");
				Forum forum = new Forum(0);
				forum.setName(objForum.getString("name"));
				forum.setDescription(objForum.getString("description"));
				forum.setCreationDate(Util.getDate(objForum.getString("creationDate"), Util.FORMATO_DATA_BD));
				forum.setAmadeus_id(objForum.getInt("id"));
				try {
					listMessage = new ArrayList<Message>();
					JSONArray objList2 = objForum.getJSONArray("messages");
					for (int j = 0; j < objList2.length(); j++) {
						JSONObject objMsg = objList2.getJSONObject(j);
						Message message = new Message(0);
						message.setAmadeus_id(objMsg.getInt("id"));
						message.setBody(objMsg.getString("body"));
						message.setDate(Util.getDate(objMsg.getString("date"), Util.FORMATO_DATA_BD));
						message.setPersonName(objMsg.getString("personName"));
						message.setPersonLogin(objMsg.getString("personLogin"));
						message.setForum(forum);
						listMessage.add(message);
					}
					forum.setListMessages(listMessage);
					forumListMessage.add(forum);
				} catch (Exception e2) {
					try {
						JSONObject objMsg = objForum.getJSONObject("messages");
						listMessage = new ArrayList<Message>();
						Message message = new Message(0);
						message.setAmadeus_id(objMsg.getInt("id"));
						message.setBody(objMsg.getString("body"));
						message.setDate(Util.getDate(objMsg.getString("date"), Util.FORMATO_DATA_BD));
						message.setPersonName(objMsg.getString("personName"));
						message.setPersonLogin(objMsg.getString("personLogin"));
						message.setForum(forum);
						
						listMessage.add(message);
						forum.setListMessages(listMessage);
						forumListMessage.add(forum);
					} catch (Exception e3) {
						
					}
					
				}
			} catch (Exception e2) {
				// TODO: handle exception
			}
			
		}
		
		return forumListMessage;
	}

	public static Message getMessageFromAmadeusWeb(String msgJson) throws JSONException{
		JSONObject jsonObj = new JSONObject(msgJson);
		JSONObject obj = jsonObj.getJSONObject("service");
		try {
			JSONObject msg = obj.getJSONObject("message");
			Message message = new Message();
			message.setAmadeus_id(msg.getInt("id"));
			return message;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}


}
